package com.xncoding.pos.service;

import com.xncoding.pos.dao.TbUserDao;
import com.xncoding.pos.model.TbUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author guoyb
 * @Date 2024/1/30  11:24
 */
@Service
@Slf4j
public class UserService {

    @Resource
    TbUserDao tbUserDao;


    // 根据id查询
    public Mono<TbUser> getById(Long id) {
        // 返回数据或空值
        return Mono.justOrEmpty(tbUserDao.queryById(id));
    }

    // 查询多个
    public Flux<TbUser> getAll() {
        return Flux.fromIterable(tbUserDao.getAll());
    }

    public List<TbUser> getList() {
        log.info("-----打印日志-----查询全部");
        return tbUserDao.getAll();
    }


    // 保存
    public Mono<Void> save(Mono<TbUser> userMono) {
        return userMono.doOnNext(user -> {
            tbUserDao.insert(user);
        }).thenEmpty(Mono.empty()); // 最后置空
    }
}
